package com.generictimebox;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.os.Build;

public class CountDownActivity extends Activity {

	boolean activeCountDown = false;
	TimeBox currentTimeBox = new TimeBox();
	ProgressBar progressBar;
	int i=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_count_down);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
	}

	public void launchCountDown(View view) {

		
		i = 0;
		progressBar=(ProgressBar)findViewById(R.id.progressBar_step);
		progressBar.setProgress(i);
		
		
		if(activeCountDown == false){
			
			GenericTimeBoxHelper.playStart();
			
			activeCountDown = true;
			Log.d("GTB","launchCountDown: " + TimeBoxManager.getInstance().getDuration());
			
		    new CountDownTimer(TimeBoxManager.getInstance().getDuration(), 500) {
			
		    	
				
		    	
		    	public void onTick(long millisUntilFinished) {

					Log.d("GTB","onTick: " + millisUntilFinished);

					i = 100 - ((int) ((millisUntilFinished) * 100 / TimeBoxManager.getInstance().getDuration()));
					Log.d("GTB","i=" + i);
					Log.d("GTB","cnt=" + millisUntilFinished);
					Log.d("GTB","tot=" + TimeBoxManager.getInstance().getDuration());

					
					
					progressBar.setProgress(i);
					
					TextView t1 = (TextView)findViewById(R.id.text_step);

					

					TimeBox newTimeBox = TimeBoxManager.getInstance().getTimeBoxAt(millisUntilFinished);

					if(currentTimeBox.equals(newTimeBox)){

						Log.d("GTB","onTick: same step");

						
					}
					else{
					
						Log.d("GTB","onTick: new step");
						GenericTimeBoxHelper.playNewStep();

					}
					currentTimeBox = newTimeBox;
					t1.setText(newTimeBox.getStepName() + " " + millisUntilFinished
							/ 1000);
					
			
			}

			public void onFinish() {
				
				Log.d("GTB","onFinish");
				
				TextView t1 = (TextView)findViewById(R.id.text_step);
				t1.setText("done!");
				
				activeCountDown = false;

			}
		}.start();
		
		
		}	else{
			Log.d("GTB","launchCountDown: already running");
			
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.count_down, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_count_down,
					container, false);
			return rootView;
		}
	}

}
